package com.ruicar.afs.cloud.bizcommon.business.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinFinancingItems;
import com.ruicar.afs.cloud.bizcommon.business.mapper.ApplyFinancingItemsMapper;
import com.ruicar.afs.cloud.bizcommon.business.service.ApplyFinancingItemsService;
import com.ruicar.afs.cloud.common.core.uid.UidGenerator;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @description: 融资项目
 * @author: sijun.yu
 * @date: 2020/5/16 11:52
 */
@Service
@AllArgsConstructor
public class ApplyFinancingItemsServiceImpl extends ServiceImpl<ApplyFinancingItemsMapper, FinFinancingItems> implements ApplyFinancingItemsService {

    private UidGenerator uidGenerator;

    /**
     * 融资项目列表
     *
     * @param applyNo
     * @param carId
     * @return
     */
    public List<FinFinancingItems> getFinancingItemsList(String applyNo, Long carId) {
        return this.baseMapper.getFinancingItemsList(applyNo, carId);
    }

    /**
     * 保存融资项目列表
     *
     * @param applyNo
     * @param itemsList
     */
    public void saveApplyFinancingItemsList(String applyNo, Long carId, List<FinFinancingItems> itemsList) {

        if (itemsList != null && itemsList.size() > 0) {
            for (FinFinancingItems itemsCondition : itemsList) {
                List<FinFinancingItems> sonList = itemsCondition.getSonList();
                FinFinancingItems financingItems = new FinFinancingItems();
                financingItems.setId(uidGenerator.getUID());
                financingItems.setApplyNo(applyNo);
                financingItems.setCarId(carId);
                financingItems.setFinanceItemCode(itemsCondition.getFinanceItemCode());
                financingItems.setFinanceItemName(itemsCondition.getFinanceItemName());
                financingItems.setFinanceItemAmt(itemsCondition.getFinanceItemAmt());
                financingItems.setFinanceItemTotal(itemsCondition.getFinanceItemTotal());
                if (itemsCondition.getFinanceItemTotal() >= 1 && sonList != null && sonList.size() > 0) {
                    financingItems.setIsParent(Boolean.TRUE);
                }
                // GPS、装潢、精品
                if("F003".equals(itemsCondition.getFinanceItemCode()) || "F006".equals(itemsCondition.getFinanceItemCode()) || "F007".equals(itemsCondition.getFinanceItemCode())){
                    if(itemsCondition.getFinanceItemAmt()!=null && itemsCondition.getFinanceItemAmt().compareTo(BigDecimal.ZERO)>0){
                        financingItems.setIsView(Boolean.TRUE);
                    }
                }
                this.baseMapper.insert(financingItems);
                // 融资子项目
                if (sonList != null && sonList.size() > 0) {
                    for (FinFinancingItems sonCondition : sonList) {
                        FinFinancingItems items = new FinFinancingItems();
                        items.setId(uidGenerator.getUID());
                        items.setUpperId(financingItems.getId());
                        items.setApplyNo(applyNo);
                        items.setCarId(carId);
                        items.setFinanceItemCode(sonCondition.getFinanceItemCode());
                        items.setFinanceItemName(sonCondition.getFinanceItemName());
                        items.setFinanceItemAmt(sonCondition.getFinanceItemAmt());
                        items.setFinanceItemTotal(sonCondition.getFinanceItemTotal());
                        if(sonCondition.getFinanceItemAmt()!=null && sonCondition.getFinanceItemAmt().compareTo(BigDecimal.ZERO)>0){
                            items.setIsView(Boolean.TRUE);
                        }
                        items.setCreateTime(new Date());
                        this.baseMapper.insert(items);
                    }
                }
            }
        }
    }

    /**
     * 通过申请编号返回融资列表
     *
     * @param applyNo
     * @return
     */
    public List<FinFinancingItems> getApplyFinancingItemsList(String applyNo) {

        List<FinFinancingItems> list = this.baseMapper.selectList(Wrappers.<FinFinancingItems>query().lambda()
                .eq(FinFinancingItems::getApplyNo, applyNo));
        return list;
    }

    /**
     * 通过申请编号和融资项目编号查询融资项目
     *
     * @param applyNo
     * @param financeItemCode
     * @return
     */
    public FinFinancingItems getApplyFinancingItems(String applyNo, String financeItemCode) {

        FinFinancingItems financingItems = this.baseMapper.selectOne(Wrappers.<FinFinancingItems>query().lambda()
                .eq(FinFinancingItems::getApplyNo, applyNo).eq(FinFinancingItems::getFinanceItemCode, financeItemCode));
        return financingItems;
    }

    /**
     * 通过申请编号、父类ID返回融资列表
     *
     * @param applyNo
     * @param upperId
     * @return
     */
    public List<FinFinancingItems> getApplyFinancingItemsList(String applyNo, Long upperId) {

        List<FinFinancingItems> list = this.baseMapper.selectList(Wrappers.<FinFinancingItems>query().lambda()
                .eq(FinFinancingItems::getApplyNo, applyNo)
                .eq(FinFinancingItems::getUpperId, upperId));
        return list;
    }

}
